home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 037a / tc256d.zip / BGIDEMO.C < prev    next >
C/C++ Source or Header  |  1991-02-19  |  42KB  |  1,457 lines

  1. /*
  2.    GRAPHICS DEMO FOR TURBO C 2.0
  3.  
  4.    Copyright (c) 1987,88,90 Borland International. All rights reserved.
  5.  
  6.    From the command line, use:
  7.  
  8.         tcc bgidemo graphics.lib
  9.  
  10. */
  11.  
  12. #ifdef __TINY__
  13. #error BGIDEMO will not run in the tiny model.
  14. #endif
  15.  
  16. #include <dos.h>
  17. #include <math.h>
  18. #include <conio.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <stdarg.h>
  22.  
  23. #include <graphics.h>
  24.  
  25. #include "vgaextra.h"
  26. #include "isvga256.h"
  27. #include "isvgadet.h"
  28.  
  29. #define ESC    0x1b            /* Define the escape key    */
  30. #define TRUE    1            /* Define some handy constants    */
  31. #define FALSE    0            /* Define some handy constants    */
  32. #define PI    3.14159         /* Define a value for PI    */
  33. #define ON    1            /* Define some handy constants    */
  34. #define OFF    0            /* Define some handy constants    */
  35.  
  36. char *Fonts[] = {
  37.   "DefaultFont",   "TriplexFont",   "SmallFont",
  38.   "SansSerifFont", "GothicFont"
  39. };
  40.  
  41. char *LineStyles[] = {
  42.   "SolidLn",  "DottedLn",  "CenterLn",  "DashedLn",  "UserBitLn"
  43. };
  44.  
  45. char *FillStyles[] = {
  46.   "EmptyFill",  "SolidFill",      "LineFill",      "LtSlashFill",
  47.   "SlashFill",  "BkSlashFill",    "LtBkSlashFill", "HatchFill",
  48.   "XHatchFill", "InterleaveFill", "WideDotFill",   "CloseDotFill"
  49. };
  50.  
  51. char *TextDirect[] = {
  52.   "HorizDir",  "VertDir"
  53. };
  54.  
  55. char *HorizJust[] = {
  56.   "LeftText",   "CenterText",   "RightText"
  57. };
  58.  
  59. char *VertJust[] = {
  60.   "BottomText",  "CenterText",  "TopText"
  61. };
  62.  
  63. struct PTS {
  64.   int x, y;
  65. };    /* Structure to hold vertex points    */
  66.  
  67. int    GraphDriver;        /* The Graphics device driver        */
  68. int    GraphMode;        /* The Graphics mode value        */
  69. double AspectRatio;        /* Aspect ratio of a pixel on the screen*/
  70. int    MaxX, MaxY;        /* The maximum resolution of the screen */
  71. int    MaxColors;        /* The maximum # of colors available    */
  72. int    ErrorCode;        /* Reports any graphics errors        */
  73. struct palettetype palette;        /* Used to read palette info    */
  74.  
  75. /*                                    */
  76. /*    Function prototypes                        */
  77. /*                                    */
  78.  
  79. void Initialize(void);
  80. void ReportStatus(void);
  81. void TextDump(void);
  82. void Bar3DDemo(void);
  83. void RandomBars(void);
  84. void TextDemo(void);
  85. void ColorDemo(void);
  86. void ArcDemo(void);
  87. void CircleDemo(void);
  88. void PieDemo(void);
  89. void BarDemo(void);
  90. void LineRelDemo(void);
  91. void PutPixelDemo(void);
  92. void PutImageDemo(void);
  93. void LineToDemo(void);
  94. void LineStyleDemo(void);
  95. void CRTModeDemo(void);
  96. void UserLineStyleDemo(void);
  97. void FillStyleDemo(void);
  98. void FillPatternDemo(void);
  99. void PaletteDemo(void);
  100. void PolyDemo(void);
  101. void SayGoodbye(void);
  102. void Pause(void);
  103. void MainWindow(char *header);
  104. void StatusLine(char *msg);
  105. void DrawBorder(void);
  106. void changetextstyle(int font, int direction, int charsize);
  107. int  gprintf(int *xloc, int *yloc, char *fmt, ... );
  108.  
  109.   DACarray Palette_Array;              /* create array to hold DAC values */
  110.  
  111.  
  112. /*                                    */
  113. /*    Begin main function                        */
  114. /*                                    */
  115.  
  116. int main()
  117. {
  118.  
  119.   Initialize();         /* Set system into Graphics mode    */
  120.   ReportStatus();        /* Report results of the initialization */
  121.  
  122.   ColorDemo();            /* Begin actual demonstration        */
  123.   if( GraphDriver==EGA || GraphDriver==EGALO || GraphDriver==VGA ||
  124.       GraphDriver==134 )        /* allow color demo with installed driver */
  125.     PaletteDemo();
  126.   PutPixelDemo();
  127.   PutImageDemo();
  128.   Bar3DDemo();
  129.   BarDemo();
  130.   RandomBars();
  131.   ArcDemo();
  132.   CircleDemo();
  133.   PieDemo();
  134.   LineRelDemo();
  135.   LineToDemo();
  136.   LineStyleDemo();
  137.   UserLineStyleDemo();
  138.   TextDump();
  139.   TextDemo();
  140.   CRTModeDemo();
  141.   FillStyleDemo();
  142.   FillPatternDemo();
  143.   PolyDemo();
  144.   SayGoodbye();         /* Give user the closing screen     */
  145.  
  146.   closegraph();         /* Return the system to text mode    */
  147.   return(0);
  148. }
  149.  
  150. /*                                    */
  151. /*    INITIALIZE: Initializes the graphics system and reports     */
  152. /*    any errors which occured.                    */
  153. /*                                    */
  154.  
  155. void Initialize(void)
  156. {
  157.   int xasp, yasp;            /* Used to read the aspect ratio*/
  158.   installuserdriver("ISVGA256",DetectISVGA256);
  159.   GraphDriver = DETECT;
  160.   initgraph( &GraphDriver, &GraphMode, "" );
  161.   ErrorCode = graphresult();        /* Read result of initialization*/
  162.   if( ErrorCode != grOk ){        /* Error occured during init    */
  163.     printf(" Graphics System Error: %s\n", grapherrormsg( ErrorCode ) );
  164.     exit( 1 );
  165.   }
  166.   getpalette( &palette );        /* Read the palette from board    */
  167.   MaxColors = getmaxcolor() + 1;    /* Read maximum number of colors*/
  168.  
  169.   MaxX = getmaxx();
  170.   MaxY = getmaxy();            /* Read size of screen        */
  171.  
  172.   getaspectratio( &xasp, &yasp );    /* read the hardware aspect    */
  173.   AspectRatio = (double)xasp / (double)yasp; /* Get correction factor    */
  174.  
  175. }
  176.  
  177. /*                                    */
  178. /*    REPORTSTATUS: Report the current configuration of the system    */
  179. /*    after the auto-detect initialization.                */
  180. /*                                    */
  181.  
  182. void ReportStatus(void)
  183. {
  184.   struct viewporttype      viewinfo;    /* Params for inquiry procedures*/
  185.   struct linesettingstype lineinfo;
  186.   struct fillsettingstype fillinfo;
  187.   struct textsettingstype textinfo;
  188.   struct palettetype      palette;
  189.  
  190.   char *driver, *mode;            /* Strings for driver and mode    */
  191.   int x, y;
  192.  
  193.   getviewsettings( &viewinfo );
  194.   getlinesettings( &lineinfo );
  195.   getfillsettings( &fillinfo );
  196.   gettextsettings( &textinfo );
  197.   getpalette( &palette );
  198.  
  199.   x = 10;
  200.   y = 4;
  201.  
  202.   MainWindow( "Status report after InitGraph" );
  203.   settextjustify( LEFT_TEXT, TOP_TEXT );
  204.  
  205.   driver = getdrivername();
  206.   mode = getmodename(GraphMode);    /* get current setting        */
  207.  
  208.   gprintf( &x, &y, "Graphics device    : %-20s (%d)", driver, GraphDriver );
  209.   gprintf( &x, &y, "Graphics mode      : %-20s (%d)", mode, GraphMode );
  210.   gprintf( &x, &y, "Screen resolution  : ( 0, 0, %d, %d )", getmaxx(), getmaxy() );
  211.  
  212.   gprintf( &x, &y, "Current view port  : ( %d, %d, %d, %d )",
  213.   viewinfo.left, viewinfo.top, viewinfo.right, viewinfo.bottom );
  214.   gprintf( &x, &y, "Clipping           : %s", viewinfo.clip ? "ON" : "OFF" );
  215.  
  216.   gprintf( &x, &y, "Current position   : ( %d, %d )", getx(), gety() );
  217.   gprintf( &x, &y, "Colors available   : %d", MaxColors );
  218.   gprintf( &x, &y, "Current color      : %d", getcolor() );
  219.  
  220.   gprintf( &x, &y, "Line style         : %s", LineStyles[ lineinfo.linestyle ] );
  221.   gprintf( &x, &y, "Line thickness     : %d", lineinfo.thickness );
  222.  
  223.   gprintf( &x, &y, "Current fill style : %s", FillStyles[ fillinfo.pattern ] );
  224.   gprintf( &x, &y, "Current fill color : %d", fillinfo.color );
  225.  
  226.   gprintf( &x, &y, "Current font       : %s", Fonts[ textinfo.font ] );
  227.   gprintf( &x, &y, "Text direction     : %s", TextDirect[ textinfo.direction ] );
  228.   gprintf( &x, &y, "Character size     : %d", textinfo.charsize );
  229.   gprintf( &x, &y, "Horizontal justify : %s", HorizJust[ textinfo.horiz ] );
  230.   gprintf( &x, &y, "Vertical justify   : %s", VertJust[ textinfo.vert ] );
  231.  
  232.   Pause();                /* Pause for user to read screen*/
  233.  
  234. }
  235.  
  236. /*                                    */
  237. /*    TEXTDUMP: Display the all the characters in each of the     */
  238. /*    available fonts.                        */
  239. /*                                    */
  240.  
  241. void TextDump()
  242. {
  243.   static int CGASizes[]  = {
  244.     1, 3, 7, 3, 3   };
  245.   static int NormSizes[] = {
  246.     1, 4, 7, 4, 4   };
  247.  
  248.   char buffer[80];
  249.   int font, ch, wwidth, lwidth, size;
  250.   struct viewporttype vp;
  251.  
  252.   for( font=0 ; font<5 ; ++font ){    /* For each available font    */
  253.     sprintf( buffer, "%s Character Set", Fonts[font] );
  254.     MainWindow( buffer );        /* Display fontname as banner    */
  255.     getviewsettings( &vp );        /* read current viewport    */
  256.  
  257.     settextjustify( LEFT_TEXT, TOP_TEXT );
  258.     moveto( 2, 3 );
  259.  
  260.     buffer[1] = '\0';                   /* Terminate string             */
  261.     wwidth = vp.right - vp.left;    /* Determine the window width    */
  262.     lwidth = textwidth( "H" );          /* Get average letter width     */
  263.  
  264.     if( font == DEFAULT_FONT ){
  265.       changetextstyle( font, HORIZ_DIR, 1 );
  266.       ch = 0;
  267.       while( ch < 256 ){        /* For each possible character    */
  268.     buffer[0] = ch;         /* Put character into a string    */
  269.     outtext( buffer );        /* send string to screen    */
  270.     if( (getx() + lwidth) > wwidth )
  271.       moveto( 2, gety() + textheight("H") + 3 );
  272.     ++ch;                /* Goto the next character    */
  273.       }
  274.     }
  275.     else{
  276.  
  277.       size = (MaxY < 200) ? CGASizes[font] : NormSizes[font];
  278.       changetextstyle( font, HORIZ_DIR, size );
  279.  
  280.       ch = '!';                         /* Begin at 1st printable       */
  281.       while( ch < 127 ){        /* For each printable character */
  282.     buffer[0] = ch;         /* Put character into a string    */
  283.     outtext( buffer );        /* send string to screen    */
  284.     if( (lwidth+getx()) > wwidth )    /* Are we still in window?    */
  285.       moveto( 2, gety()+textheight("H")+3 );
  286.     ++ch;                /* Goto the next character    */
  287.       }
  288.  
  289.     }
  290.  
  291.     Pause();                /* Pause until user acks    */
  292.  
  293.   }                    /* End of FONT loop        */
  294.  
  295. }
  296.  
  297. /*                                    */
  298. /*    BAR3DDEMO: Display a 3-D bar chart on the screen.        */
  299. /*                                    */
  300.  
  301. void Bar3DDemo(void)
  302. {
  303.   static int barheight[] = {
  304.     1, 3, 5, 4, 3, 2, 1, 5, 4, 2, 3   };
  305.   struct viewporttype vp;
  306.   int xstep, ystep;
  307.   int i, j, h, color, bheight;
  308.   char buffer[10];
  309.  
  310.   MainWindow( "Bar 3-D / Rectangle Demonstration" );
  311.  
  312.   h = 3 * textheight( "H" );
  313.   getviewsettings( &vp );
  314.   settextjustify( CENTER_TEXT, TOP_TEXT );
  315.   changetextstyle( TRIPLEX_FONT, HORIZ_DIR, 4 );
  316.   outtextxy( MaxX/2, 6, "These are 3-D Bars" );
  317.   changetextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );
  318.   setviewport( vp.left+50, vp.top+40, vp.right-50, vp.bottom-10, 1 );
  319.   getviewsettings( &vp );
  320.  
  321.   line( h, h, h, vp.bottom-vp.top-h );
  322.   line( h, (vp.bottom-vp.top)-h, (vp.right-vp.left)-h, (vp.bottom-vp.top)-h );
  323.   xstep = ((vp.right-vp.left) - (2*h)) / 10;
  324.   ystep = ((vp.bottom-vp.top) - (2*h)) / 5;
  325.   j = (vp.bottom-vp.top) - h;
  326.   settextjustify( CENTER_TEXT, CENTER_TEXT );
  327.  
  328.   for( i=0 ; i<6 ; ++i ){
  329.     line( h/2, j, h, j );
  330.     itoa( i, buffer, 10 );
  331.     outtextxy( 0, j, buffer );
  332.     j -= ystep;
  333.   }
  334.  
  335.   j = h;
  336.   settextjustify( CENTER_TEXT, TOP_TEXT );
  337.  
  338.   for( i=0 ; i<11 ; ++i ){
  339.     color = random( MaxColors );
  340.     setfillstyle( 1, color );
  341.     line( j, (vp.bottom-vp.top)-h, j, (vp.bottom-vp.top-3)-(h/2) );
  342.     itoa( i, buffer, 10 );
  343.     outtextxy( j, (vp.bottom-vp.top)-(h/2), buffer );
  344.     if( i != 10 ){
  345.       bheight = (vp.bottom-vp.top) - h - 1;
  346.       bar3d( j, (vp.bottom-vp.top-h)-(barheight[i]*ystep), j+xstep, bheight, 15, 1 );
  347.     }
  348.     j += xstep;
  349.   }
  350.  
  351.   Pause();                /* Pause for user's response    */
  352.  
  353. }
  354.  
  355. /*                                    */
  356. /*    RANDOMBARS: Display random bars                 */
  357. /*                                    */
  358.  
  359. void RandomBars(void)
  360. {
  361.   int color;
  362.  
  363.   MainWindow( "Random Bars" );
  364.   StatusLine( "Esc aborts or press a key..." ); /* Put msg at bottom of screen   */
  365.   while( !kbhit() ){            /* Until user enters a key...    */
  366.     color = random( MaxColors-1 )+1;
  367.     setcolor( color );
  368.     setfillstyle( 1, color );
  369.     bar3d( random( getmaxx() ), random( getmaxy() ),
  370.        random( getmaxx() ), random( getmaxy() ), 0, OFF);
  371.   }
  372.  
  373.   Pause();                /* Pause for user's response    */
  374.  
  375. }
  376.  
  377.  
  378. /*                                    */
  379. /*    TEXTDEMO: Show each font in several sizes to the user.        */
  380. /*                                    */
  381.  
  382. void TextDemo(void)
  383. {
  384.   int charsize[] = {
  385.     1, 3, 7, 3, 4   };
  386.   int font, size;
  387.   int h, x, y, i;
  388.   struct viewporttype vp;
  389.   char buffer[80];
  390.  
  391.   for( font=0 ; font<5 ; ++font ){    /* For each of the four fonts    */
  392.  
  393.     sprintf( buffer, "%s Demonstration", Fonts[font] );
  394.     MainWindow( buffer );
  395.     getviewsettings( &vp );
  396.  
  397.     changetextstyle( font, VERT_DIR, charsize[font] );
  398.     settextjustify( CENTER_TEXT, BOTTOM_TEXT );
  399.     outtextxy( 2*textwidth("M"), vp.bottom - 2*textheight("M"), "Vertical" );
  400.  
  401.     changetextstyle( font, HORIZ_DIR, charsize[font] );
  402.     settextjustify( LEFT_TEXT, TOP_TEXT );
  403.     outtextxy( 2*textwidth("M"), 2, "Horizontal" );
  404.  
  405.     settextjustify( CENTER_TEXT, CENTER_TEXT );
  406.     x = (vp.right - vp.left) / 2;
  407.     y = textheight( "H" );
  408.  
  409.     for( i=1 ; i<5 ; ++i ){        /* For each of the sizes */
  410.       size = (font == SMALL_FONT) ? i+3 : i;
  411.       changetextstyle( font, HORIZ_DIR, size );
  412.       h = textheight( "H" );
  413.       y += h;
  414.       sprintf( buffer, "Size %d", size );
  415.       outtextxy( x, y, buffer );
  416.  
  417.     }
  418.  
  419.     if( font != DEFAULT_FONT ){     /* Show user declared font size */
  420.       y += h / 2;            /* Move down the screen     */
  421.       settextjustify( CENTER_TEXT, TOP_TEXT );
  422.       setusercharsize( 5, 6, 3, 2 );
  423.       changetextstyle( font, HORIZ_DIR, USER_CHAR_SIZE );
  424.       outtextxy( (vp.right-vp.left)/2, y, "User Defined Size" );
  425.     }
  426.  
  427.     Pause();                /* Pause to let user look    */
  428.  
  429.   }                    /* End of FONT loop        */
  430.  
  431. }
  432.  
  433. /*                                    */
  434. /*    COLORDEMO: Display the current color palette on the screen.    */
  435. /*                                    */
  436.  
  437. void ColorDemo(void)
  438. {
  439.   struct viewporttype vp;
  440.   int color, height, width;
  441.   int x, y, i, j;
  442.   char cnum[5];
  443.  
  444.   MainWindow( "Color Demonstration" );  /* Show demonstration name      */
  445.  
  446.   color = 1;
  447.   getviewsettings( &vp );        /* Get the current window size    */
  448.   width  = 2 * ( (vp.right+1) / 16 );       /* Get box dimensions       */
  449.   height = 2 * ( (vp.bottom-10) / 10 );
  450.  
  451.   x = width / 2;
  452.   y = height / 2;    /* Leave 1/2 box border     */
  453.  
  454.   for( j=0 ; j<3 ; ++j ){        /* Row loop            */
  455.  
  456.     for( i=0 ; i<5 ; ++i ){        /* Column loop            */
  457.  
  458.       setfillstyle(SOLID_FILL, color);    /* Set to solid fill in color    */
  459.       setcolor( color );        /* Set the same border color    */
  460.  
  461.       bar( x, y, x+width, y+height );    /* Draw the rectangle        */
  462.       rectangle( x, y, x+width, y+height );  /* outline the rectangle    */
  463.  
  464.       if( color == BLACK ){        /* If box was black...        */
  465.     setcolor( WHITE );        /* Set drawing color to white    */
  466.     rectangle( x, y, x+width, y+height );  /* Outline black in white*/
  467.       }
  468.  
  469.       itoa( color, cnum, 10 );        /* Convert # to ASCII        */
  470.       outtextxy( x+(width/2), y+height+4, cnum );  /* Show color #    */
  471.  
  472.       color = ++color % MaxColors;    /* Advance to the next color    */
  473.       x += (width / 2) * 3;        /* move the column base     */
  474.     }                /* End of Column loop        */
  475.  
  476.     y += (height / 2) * 3;        /* move the row base        */
  477.     x = width / 2;            /* reset column base        */
  478.   }                    /* End of Row loop        */
  479.  
  480.   Pause();                /* Pause for user's response    */
  481.  
  482. }
  483.  
  484. /*                                    */
  485. /*    ARCDEMO: Display a random pattern of arcs on the screen */
  486. /*    until the user says enough.                    */
  487. /*                                    */
  488.  
  489. void ArcDemo(void)
  490. {
  491.   int mradius;                /* Maximum radius allowed    */
  492.   int eangle;                /* Random end angle of Arc    */
  493.   struct arccoordstype ai;        /* Used to read Arc Cord info    */
  494.  
  495.   MainWindow( "Arc Demonstration" );
  496.   StatusLine( "ESC Aborts - Press a Key to stop" );
  497.  
  498.   mradius = MaxY / 10;            /* Determine the maximum radius */
  499.  
  500.   while( !kbhit() ){            /* Repeat until a key is hit    */
  501.     setcolor( random( MaxColors - 1 ) + 1 );    /* Randomly select a color    */
  502.     eangle = random( 358 ) + 1;     /* Select an end angle        */
  503.     arc( random(MaxX), random(MaxY), random(eangle), eangle, mradius );
  504.     getarccoords( &ai );        /* Read Cord data        */
  505.     line( ai.x, ai.y, ai.xstart, ai.ystart ); /* line from start to center */
  506.     line( ai.x, ai.y,    ai.xend,   ai.yend ); /* line from end to center   */
  507.   }                    /* End of WHILE not KBHIT    */
  508.  
  509.   Pause();                /* Wait for user's response     */
  510.  
  511. }
  512.  
  513. /*                                    */
  514. /*    CIRCLEDEMO: Display a random pattern of circles on the screen    */
  515. /*    until the user says enough.                    */
  516. /*                                    */
  517.  
  518. void CircleDemo(void)
  519. {
  520.   int mradius;                /* Maximum radius allowed    */
  521.  
  522.   MainWindow( "Circle Demonstration" );
  523.   StatusLine( "ESC Aborts - Press a Key to stop" );
  524.  
  525.   mradius = MaxY / 10;            /* Determine the maximum radius */
  526.  
  527.   while( !kbhit() ){            /* Repeat until a key is hit    */
  528.     setcolor( random( MaxColors - 1 ) + 1 );    /* Randomly select a color    */
  529.     circle( random(MaxX), random(MaxY), random(mradius) );
  530.   }                    /* End of WHILE not KBHIT    */
  531.  
  532.   Pause();                /* Wait for user's response     */
  533.  
  534. }
  535.  
  536. /*                                    */
  537. /*    PIEDEMO: Display a pie chart on the screen.            */
  538. /*                                    */
  539.  
  540. #define adjasp( y )    ((int)(AspectRatio * (double)(y)))
  541. #define torad( d )    (( (double)(d) * PI ) / 180.0 )
  542.  
  543. void PieDemo(void)
  544. {
  545.   struct viewporttype vp;
  546.   int xcenter, ycenter, radius, lradius;
  547.   int x, y;
  548.   double radians, piesize;
  549.  
  550.   MainWindow( "Pie Chart Demonstration" );
  551.  
  552.   getviewsettings( &vp );        /* Get the current viewport    */
  553.   xcenter = (vp.right - vp.left) / 2;    /* Center the Pie horizontally    */
  554.   ycenter = (vp.bottom - vp.top) / 2+20;/* Center the Pie vertically    */
  555.   radius  = (vp.bottom - vp.top) / 3;    /* It will cover 2/3rds screen    */
  556.   piesize = (vp.bottom - vp.top) / 4.0; /* Optimum height ratio of pie    */
  557.  
  558.   while( (AspectRatio*radius) < piesize ) ++radius;
  559.  
  560.   lradius = radius + ( radius / 5 );    /* Labels placed 20% farther    */
  561.  
  562.   changetextstyle( TRIPLEX_FONT, HORIZ_DIR, 4 );
  563.   settextjustify( CENTER_TEXT, TOP_TEXT );
  564.   outtextxy( MaxX/2, 6, "This is a Pie Chart" );
  565.   changetextstyle( TRIPLEX_FONT, HORIZ_DIR, 1 );
  566.   settextjustify( CENTER_TEXT, TOP_TEXT );
  567.  
  568.   setfillstyle( SOLID_FILL, RED );
  569.   pieslice( xcenter+10, ycenter-adjasp(10), 0, 90, radius );
  570.   radians = torad( 45 );
  571.   x = xcenter + (int)( cos( radians ) * (double)lradius );
  572.   y = ycenter - (int)( sin( radians ) * (double)lradius * AspectRatio );
  573.   settextjustify( LEFT_TEXT, BOTTOM_TEXT );
  574.   outtextxy( x, y, "25 %" );
  575.  
  576.   setfillstyle( WIDE_DOT_FILL, GREEN );
  577.   pieslice( xcenter, ycenter, 90, 135, radius );
  578.   radians = torad( 113 );
  579.   x = xcenter + (int)( cos( radians ) * (double)lradius );
  580.   y = ycenter - (int)( sin( radians ) * (double)lradius * AspectRatio );
  581.   settextjustify( RIGHT_TEXT, BOTTOM_TEXT );
  582.   outtextxy( x, y, "12.5 %" );
  583.  
  584.   setfillstyle( INTERLEAVE_FILL, YELLOW );
  585.   settextjustify( RIGHT_TEXT, CENTER_TEXT );
  586.   pieslice( xcenter-10, ycenter, 135, 225, radius );
  587.   radians = torad( 180 );
  588.   x = xcenter + (int)( cos( radians ) * (double)lradius );
  589.   y = ycenter - (int)( sin( radians ) * (double)lradius * AspectRatio );
  590.   settextjustify( RIGHT_TEXT, CENTER_TEXT );
  591.   outtextxy( x, y, "25 %" );
  592.  
  593.   setfillstyle( 1, BLUE );
  594.   pieslice( xcenter, ycenter, 225, 360, radius );
  595.   radians = torad( 293 );
  596.   x = xcenter + (int)( cos( radians ) * (double)lradius );
  597.   y = ycenter - (int)( sin( radians ) * (double)lradius * AspectRatio );
  598.   settextjustify( LEFT_TEXT, TOP_TEXT );
  599.   outtextxy( x, y, "37.5 %" );
  600.  
  601.   Pause();                /* Pause for user's response    */
  602.  
  603. }
  604.  
  605. /*                                    */
  606. /*    BARDEMO: Draw a 2-D bar chart using Bar and Rectangle.        */
  607. /*                                    */
  608.  
  609. void BarDemo(void)
  610. {
  611.   int barheight[] = {
  612.     1, 3, 5, 2, 4   };
  613.   int styles[]      = {
  614.     1, 3, 10, 5, 9, 1    };
  615.   int xstep, ystep;
  616.   int sheight, swidth;
  617.   int i, j, h;
  618.   struct viewporttype vp;
  619.   char buffer[40];
  620.  
  621.   MainWindow( "Bar / Rectangle demonstration" );
  622.   h = 3 * textheight( "H" );
  623.   getviewsettings( &vp );
  624.   settextjustify( CENTER_TEXT, TOP_TEXT );
  625.   changetextstyle( TRIPLEX_FONT, HORIZ_DIR, 4 );
  626.   outtextxy( MaxX /2, 6, "These are 2-D Bars" );
  627.   changetextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );
  628.   setviewport( vp.left+50, vp.top+30, vp.right-50, vp.bottom-10, 1 );
  629.  
  630.   getviewsettings( &vp );
  631.   sheight = vp.bottom - vp.top;
  632.   swidth  = vp.right  - vp.left;
  633.  
  634.   line( h, h, h, sheight-h );
  635.   line( h, sheight-h, sheight-h, sheight-h );
  636.   ystep = (sheight - (2*h) ) / 5;
  637.   xstep = (swidth  - (2*h) ) / 5;
  638.   j = sheight - h;
  639.   settextjustify( CENTER_TEXT, CENTER_TEXT );
  640.  
  641.   for( i=0 ; i<6 ; ++i ){
  642.     line( h/2, j, h, j );
  643.     itoa( i, buffer, 10 );
  644.     outtextxy( 0, j, buffer );
  645.     j -= ystep;
  646.   }
  647.  
  648.   j = h;
  649.   settextjustify( CENTER_TEXT, TOP_TEXT );
  650.   for( i=0 ; i<6 ; ++i ){
  651.     setfillstyle(1, random(MaxColors) );
  652.     line( j, sheight - h, j, sheight- 3 - (h/2) );
  653.     itoa( i, buffer, 10 );
  654.     outtextxy( j, sheight - (h/2), buffer );
  655.     if( i != 5 ){
  656.       bar( j, (sheight-h)-(barheight[i] * ystep), j+xstep, sheight-h-1 );
  657.       rectangle( j, (sheight-h)-(barheight[i] * ystep), j+xstep, sheight-h);
  658.     }
  659.     j += xstep;
  660.   }
  661.  
  662.   Pause();
  663.  
  664. }
  665.  
  666. /*                                    */
  667. /*    LINERELDEMO: Display pattern using moverel and linerel cmds.    */
  668. /*                                    */
  669.  
  670. void LineRelDemo(void)
  671. {
  672.   struct viewporttype vp;
  673.   int h, w, dx, dy, cx, cy;
  674.   struct PTS outs[7];
  675.  
  676.  
  677.   MainWindow( "MoveRel / LineRel Demonstration" );
  678.   StatusLine( "Press any key to continue, ESC to Abort" );
  679.  
  680.   getviewsettings( &vp );
  681.   cx = (vp.right  - vp.left) / 2;    /* Center of the screen coords    */
  682.   cy = (vp.bottom - vp.top ) / 2;
  683.  
  684.   h  = (vp.bottom - vp.top ) / 8;
  685.   w  = (vp.right  - vp.left) / 9;
  686.  
  687.   dx = 2 * w;
  688.   dy = 2 * h;
  689.  
  690.   setcolor( BLACK );
  691.  
  692.   setfillstyle( SOLID_FILL, BLUE );
  693.   bar( 0, 0, vp.right-vp.left, vp.bottom-vp.top );    /* Draw backgnd */
  694.  
  695.   outs[0].x = cx -  dx;
  696.   outs[0].y = cy -  dy;
  697.   outs[1].x = cx - (dx-w);
  698.   outs[1].y = cy - (dy+h);
  699.   outs[2].x = cx +  dx;
  700.   outs[2].y = cy - (dy+h);
  701.   outs[3].x = cx +  dx;
  702.   outs[3].y = cy +  dy;
  703.   outs[4].x = cx + (dx-w);
  704.   outs[4].y = cy + (dy+h);
  705.   outs[5].x = cx -  dx;
  706.   outs[5].y = cy + (dy+h);
  707.   outs[6].x = cx -  dx;
  708.   outs[6].y = cy -  dy;
  709.  
  710.   setfillstyle( SOLID_FILL, WHITE );
  711.   fillpoly( 7, (int far *)outs );
  712.  
  713.   outs[0].x = cx - (w/2);
  714.   outs[0].y = cy + h;
  715.   outs[1].x = cx + (w/2);
  716.   outs[1].y = cy + h;
  717.   outs[2].x = cx + (w/2);
  718.   outs[2].y = cy - h;
  719.   outs[3].x = cx - (w/2);
  720.   outs[3].y = cy - h;
  721.   outs[4].x = cx - (w/2);
  722.   outs[4].y = cy + h;
  723.  
  724.   setfillstyle( SOLID_FILL, BLUE );
  725.   fillpoly( 5, (int far *)outs );
  726.  
  727.   /*    Draw a Tesseract object on the screen using the LineRel and    */
  728.   /*    MoveRel drawing commands.                    */
  729.  
  730.   moveto( cx-dx, cy-dy );
  731.   linerel(  w, -h );
  732.   linerel(  3*w,    0 );
  733.   linerel(   0,  5*h );
  734.   linerel( -w,    h );
  735.   linerel( -3*w,    0 );
  736.   linerel(   0, -5*h );
  737.  
  738.   moverel( w, -h );
  739.   linerel(   0,  5*h );
  740.   linerel( w+(w/2), 0 );
  741.   linerel(   0, -3*h );
  742.   linerel( w/2,   -h );
  743.   linerel( 0, 5*h );
  744.  
  745.   moverel(  0, -5*h );
  746.   linerel( -(w+(w/2)), 0 );
  747.   linerel( 0, 3*h );
  748.   linerel( -w/2, h );
  749.  
  750.   moverel( w/2, -h );
  751.   linerel( w, 0 );
  752.  
  753.   moverel( 0, -2*h );
  754.   linerel( -w, 0 );
  755.  
  756.   Pause();                /* Wait for user's response     */
  757.  
  758. }
  759.  
  760. /*                                    */
  761. /*    PUTPIXELDEMO: Display a pattern of random dots on the screen    */
  762. /*    and pick them back up again.                    */
  763. /*                                    */
  764.  
  765. void PutPixelDemo(void)
  766. {
  767.   int seed = 1958;
  768.   int i, x, y, h, w, color;
  769.   struct viewporttype vp;
  770.  
  771.   MainWindow( "PutPixel / GetPixel Demonstration" );
  772.  
  773.   getviewsettings( &vp );
  774.   h = vp.bottom - vp.top;
  775.   w = vp.right    - vp.left;
  776.  
  777.   srand( seed );            /* Restart random # function    */
  778.  
  779.   for( i=0 ; i<5000 ; ++i ){        /* Put 5000 pixels on screen    */
  780.     x = 1 + random( w - 1 );        /* Generate a random location    */
  781.     y = 1 + random( h - 1 );
  782.     color = random( MaxColors );
  783.     putpixel( x, y, color );
  784.   }
  785.  
  786.   srand( seed );            /* Restart Random # at same #    */
  787.  
  788.   for( i=0 ; i<5000 ; ++i ){        /* Take the 5000 pixels off    */
  789.     x = 1 + random( w - 1 );        /* Generate a random location    */
  790.     y = 1 + random( h - 1 );
  791.     color = getpixel( x, y );        /* Read the color pixel     */
  792.     if( color == random( MaxColors ) )    /* Used to keep RANDOM in sync    */
  793.       putpixel( x, y, 0 );        /* Write pixel to BLACK     */
  794.   }
  795.  
  796.   Pause();                /* Wait for user's response     */
  797.  
  798. }
  799.  
  800. /*                                    */
  801. /*   PUTIMAGEDEMO                            */
  802. /*                                    */
  803. void PutImageDemo(void)
  804. {
  805.   static int r        = 20;
  806.   static int StartX = 100;
  807.   static int StartY = 50;
  808.  
  809.   struct viewporttype vp;
  810.   int PauseTime, x, y, ulx, uly, lrx, lry, size, i, width, height, step;
  811.   void *Saucer;
  812.  
  813.   MainWindow("GetImage / PutImage Demonstration");
  814.   getviewsettings( &vp );
  815.  
  816.   /* Draw Saucer */
  817.   setfillstyle( SOLID_FILL, random(getmaxcolor()) );
  818.   fillellipse(StartX, StartY, r, (r/3)+2);
  819.   ellipse(StartX, StartY-4, 190, 357, r, r/3);
  820.  
  821.   line(StartX+7, StartY-6, StartX+10, StartY-12);
  822.   circle(StartX+10, StartY-12, 2);
  823.   line(StartX-7, StartY-6, StartX-10, StartY-12);
  824.   circle(StartX-10, StartY-12, 2);
  825.  
  826.  
  827.   /* Read saucer image */
  828.   ulx = StartX-(r+1);
  829.   uly = StartY-14;
  830.   lrx = StartX+(r+1);
  831.   lry = StartY+(r/3)+3;
  832.   width = lrx - ulx + 1;
  833.   height = lry - uly + 1;
  834.   size = imagesize(ulx, uly, lrx, lry);
  835.  
  836.   Saucer = malloc( size );
  837.   getimage(ulx, uly, lrx, lry, Saucer);
  838.   putimage(ulx, uly, Saucer, XOR_PUT);
  839.  
  840. /* Plot some "stars"  */
  841.   for ( i=0 ; i<1000; ++i )
  842.     putpixel(random(MaxX), random(MaxY), random( MaxColors-1 )+1);
  843.   x = MaxX / 2;
  844.   y = MaxY / 2;
  845.   PauseTime = 70;
  846.  
  847.   /* until a key is hit */
  848.   while ( !kbhit() ) {
  849.  
  850.     /* Draw the Saucer */
  851.     putimage(x, y, Saucer, XOR_PUT);             /*  draw image  */
  852.     delay(PauseTime);
  853.     putimage(x, y, Saucer, XOR_PUT);             /* erase image  */
  854.  
  855.     /* Move Saucer */
  856.  
  857.     step = random( 2*r );
  858.     if ((step/2) % 2 != 0 )
  859.       step = -1 * step;
  860.     x = x + step;
  861.     step = random( r );
  862.     if ((step/2) % 2 != 0 )
  863.       step = -1 * step;
  864.     y = y + step;
  865.  
  866.     if (vp.left + x + width - 1 > vp.right)
  867.       x = vp.right-vp.left-width + 1;
  868.     else
  869.       if (x < 0)
  870.     x = 0;
  871.     if (vp.top + y + height - 1 > vp.bottom)
  872.       y = vp.bottom-vp.top-height + 1;
  873.     else
  874.       if (y < 0)
  875.     y = 0;
  876.   }
  877.   free( Saucer );
  878.   Pause();
  879. }
  880.  
  881.  
  882. /*                                    */
  883. /*    LINETODEMO: Display a pattern using moveto and lineto commands. */
  884. /*                                    */
  885.  
  886. #define MAXPTS    15
  887.  
  888. void LineToDemo(void)
  889. {
  890.   struct viewporttype vp;
  891.   struct PTS points[MAXPTS];
  892.   int i, j, h, w, xcenter, ycenter;
  893.   int radius, angle, step;
  894.   double  rads;
  895.  
  896.   MainWindow( "MoveTo / LineTo Demonstration" );
  897.  
  898.   getviewsettings( &vp );
  899.   h = vp.bottom - vp.top;
  900.   w = vp.right    - vp.left;
  901.  
  902.   xcenter = w / 2;            /* Determine the center of circle */
  903.   ycenter = h / 2;
  904.   radius  = (h - 30) / (AspectRatio * 2);
  905.   step      = 360 / MAXPTS;        /* Determine # of increments    */
  906.  
  907.   angle = 0;                /* Begin at zero degrees    */
  908.   for( i=0 ; i<MAXPTS ; ++i ){        /* Determine circle intercepts    */
  909.     rads = (double)angle * PI / 180.0;    /* Convert angle to radians    */
  910.     points[i].x = xcenter + (int)( cos(rads) * radius );
  911.     points[i].y = ycenter - (int)( sin(rads) * radius * AspectRatio );
  912.     angle += step;            /* Move to next increment    */
  913.   }
  914.  
  915.   circle( xcenter, ycenter, radius );    /* Draw bounding circle     */
  916.  
  917.   for( i=0 ; i<MAXPTS ; ++i ){        /* Draw the cords to the circle */
  918.     for( j=i ; j<MAXPTS ; ++j ){    /* For each remaining intersect */
  919.       moveto(points[i].x, points[i].y); /* Move to beginning of cord    */
  920.       lineto(points[j].x, points[j].y); /* Draw the cord        */
  921.     }
  922.   }
  923.  
  924.   Pause();                /* Wait for user's response     */
  925.  
  926. }
  927.  
  928. /*                                    */
  929. /*    LINESTYLEDEMO: Display a pattern using all of the standard    */
  930. /*    line styles that are available.                 */
  931. /*                                    */
  932.  
  933. void LineStyleDemo(void)
  934. {
  935.   int style, step;
  936.   int x, y, w;
  937.   struct viewporttype vp;
  938.   char buffer[40];
  939.  
  940.   MainWindow( "Pre-defined line styles" );
  941.  
  942.   getviewsettings( &vp );
  943.   w = vp.right    - vp.left;
  944.  
  945.   x = 35;
  946.   y = 10;
  947.   step = w / 11;
  948.  
  949.   settextjustify( LEFT_TEXT, TOP_TEXT );
  950.   outtextxy( x, y, "Normal Width" );
  951.  
  952.   settextjustify( CENTER_TEXT, TOP_TEXT );
  953.  
  954.   for( style=0 ; style<4 ; ++style ){
  955.     setlinestyle( style, 0, NORM_WIDTH );
  956.     line( x, y+20, x, vp.bottom-40 );
  957.     itoa( style, buffer, 10 );
  958.     outtextxy( x, vp.bottom-30, buffer );
  959.     x += step;
  960.   }
  961.  
  962.   x += 2 * step;
  963.  
  964.   settextjustify( LEFT_TEXT, TOP_TEXT );
  965.   outtextxy( x, y, "Thick Width" );
  966.   settextjustify( CENTER_TEXT, TOP_TEXT );
  967.  
  968.   for( style=0 ; style<4 ; ++style ){
  969.     setlinestyle( style, 0, THICK_WIDTH );
  970.     line( x, y+20, x, vp.bottom-40 );
  971.     itoa( style, buffer, 10 );
  972.     outtextxy( x, vp.bottom-30, buffer );
  973.     x += step;
  974.   }
  975.  
  976.   settextjustify( LEFT_TEXT, TOP_TEXT );
  977.  
  978.   Pause();                /* Wait for user's response     */
  979.  
  980. }
  981.  
  982. /*                                    */
  983. /*    CRTMODEDEMO: Demonstrate the effects of the change mode     */
  984. /*    commands on the current screen.                 */
  985. /*                                    */
  986.  
  987. void CRTModeDemo(void)
  988. {
  989.   struct viewporttype vp;
  990.   int mode;
  991.  
  992.   MainWindow( "SetGraphMode / RestoreCRTMode demo" );
  993.   getviewsettings( &vp );
  994.   mode = getgraphmode();
  995.   settextjustify( CENTER_TEXT, CENTER_TEXT );
  996.  
  997.   outtextxy( (vp.right-vp.left)/2, (vp.bottom-vp.top)/2,
  998.   "Now you are in graphics mode..." );
  999.   StatusLine( "Press any key for text mode..." );
  1000.   getch();
  1001.  
  1002.   restorecrtmode();
  1003.   printf( "Now you are in text mode.\n\n" );
  1004.   printf( "Press any key to go back to graphics..." );
  1005.   getch();
  1006.  
  1007.   setgraphmode( mode );
  1008.   MainWindow( "SetGraphMode / RestoreCRTMode demo" );
  1009.   settextjustify( CENTER_TEXT, CENTER_TEXT );
  1010.   outtextxy( (vp.right-vp.left)/2, (vp.bottom-vp.top)/2,
  1011.   "Back in Graphics Mode..." );
  1012.  
  1013.   Pause();                /* Wait for user's response     */
  1014.  
  1015. }
  1016.  
  1017. /*                                    */
  1018. /*    USERLINESTYLEDEMO: Display line styles showing the user     */
  1019. /*    defined line style functions.                    */
  1020. /*                                    */
  1021.  
  1022. void UserLineStyleDemo(void)
  1023. {
  1024.   int x, y, i, h, flag;
  1025.   unsigned int style;
  1026.   struct viewporttype vp;
  1027.  
  1028.   MainWindow( "User defined line styles" );
  1029.  
  1030.   getviewsettings( &vp );
  1031.   h = vp.bottom - vp.top;
  1032.  
  1033.   x = 4;
  1034.   y = 10;
  1035.   style = 0;
  1036.   i = 0;
  1037.  
  1038.   settextjustify( CENTER_TEXT, TOP_TEXT );
  1039.   flag = TRUE;                /* Set the bits in this pass    */
  1040.  
  1041.   while( x < vp.right-2 ){        /* Draw lines across the screen */
  1042.  
  1043.     if( flag )                /* If flag, set bits...     */
  1044.       style = style | (1 << i);     /*    Set the Ith bit in word    */
  1045.     else                /* If no flag, clear bits    */
  1046.     style = style & !(0x8000 >> i);    /*    Clear the Ith bit in word */
  1047.  
  1048.     setlinestyle( USERBIT_LINE, style, NORM_WIDTH );
  1049.     line( x, y, x, h-y );        /* Draw the new line pattern    */
  1050.  
  1051.     x += 5;                /* Move the X location of line    */
  1052.     i = ++i % 16;            /* Advance to next bit pattern    */
  1053.  
  1054.     if( style == 0xffff ){        /* Are all bits set?        */
  1055.       flag = FALSE;            /*   begin removing bits    */
  1056.       i = 0;                /* Start with whole pattern    */
  1057.     }
  1058.     else{                /* Bits not all set...        */
  1059.       if( style == 0 )            /* Are all bits clear?        */
  1060.     flag = TRUE;            /*   begin setting bits     */
  1061.     }
  1062.   }
  1063.  
  1064.   settextjustify( LEFT_TEXT, TOP_TEXT );
  1065.  
  1066.   Pause();                /* Wait for user's response     */
  1067.  
  1068. }
  1069.  
  1070. /*                                    */
  1071. /*    FILLSTYLEDEMO: Display the standard fill patterns available.    */
  1072. /*                                    */
  1073.  
  1074. void FillStyleDemo(void)
  1075. {
  1076.   int h, w, style;
  1077.   int i, j, x, y;
  1078.   struct viewporttype vp;
  1079.   char buffer[40];
  1080.  
  1081.   MainWindow( "Pre-defined Fill Styles" );
  1082.  
  1083.   getviewsettings( &vp );
  1084.   w = 2 * ((vp.right  +  1) / 13);
  1085.   h = 2 * ((vp.bottom - 10) / 10);
  1086.  
  1087.   x = w / 2;
  1088.   y = h / 2;        /* Leave 1/2 blk margin     */
  1089.   style = 0;
  1090.  
  1091.   for( j=0 ; j<3 ; ++j ){        /* Three rows of boxes        */
  1092.     for( i=0 ; i<4 ; ++i ){        /* Four column of boxes     */
  1093.       setfillstyle(style++, MaxColors-1); /* Set the fill style and WHITE */
  1094.       bar( x, y, x+w, y+h );        /* Draw the actual box        */
  1095.       rectangle( x, y, x+w, y+h );    /* Outline the box        */
  1096.       itoa( style, buffer, 10 );    /* Convert style 3 to ASCII    */
  1097.       outtextxy( x+(w / 2), y+h+4, buffer );
  1098.       ++style;                /* Go on to next style #    */
  1099.       x += (w / 2) * 3;         /* Go to next column        */
  1100.     }                /* End of coulmn loop        */
  1101.     x = w / 2;                /* Put base back to 1st column    */
  1102.     y += (h / 2) * 3;            /* Advance to next row        */
  1103.   }                    /* End of Row loop        */
  1104.  
  1105.   settextjustify( LEFT_TEXT, TOP_TEXT );
  1106.  
  1107.   Pause();                /* Wait for user's response     */
  1108.  
  1109. }
  1110.  
  1111. /*                                    */
  1112. /*    FILLPATTERNDEMO: Demonstrate how to use the user definable    */
  1113. /*    fill patterns.                            */
  1114. /*                                    */
  1115.  
  1116. void FillPatternDemo(void)
  1117. {
  1118.   int style;
  1119.   int h, w;
  1120.   int x, y, i, j;
  1121.   char buffer[40];
  1122.   struct viewporttype vp;
  1123.   static char patterns[][8] = {
  1124.     { 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55 },
  1125.     { 0x33, 0x33, 0xCC, 0xCC, 0x33, 0x33, 0xCC, 0xCC },
  1126.     { 0xF0, 0xF0, 0xF0, 0xF0, 0x0F, 0x0F, 0x0F, 0x0F },
  1127.     { 0x00, 0x10, 0x28, 0x44, 0x28, 0x10, 0x00, 0x00 },
  1128.     { 0x00, 0x70, 0x20, 0x27, 0x24, 0x24, 0x07, 0x00 },
  1129.     { 0x00, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00 },
  1130.     { 0x00, 0x00, 0x3C, 0x3C, 0x3C, 0x3C, 0x00, 0x00 },
  1131.     { 0x00, 0x7E, 0x7E, 0x7E, 0x7E, 0x7E, 0x7E, 0x00 },
  1132.     { 0x00, 0x00, 0x22, 0x08, 0x00, 0x22, 0x1C, 0x00 },
  1133.     { 0xFF, 0x7E, 0x3C, 0x18, 0x18, 0x3C, 0x7E, 0xFF },
  1134.     { 0x00, 0x10, 0x10, 0x7C, 0x10, 0x10, 0x00, 0x00 },
  1135.     { 0x00, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x00 }
  1136.   };
  1137.  
  1138.   MainWindow( "User Defined Fill Styles" );
  1139.  
  1140.   getviewsettings( &vp );
  1141.   w = 2 * ((vp.right  +  1) / 13);
  1142.   h = 2 * ((vp.bottom - 10) / 10);
  1143.  
  1144.   x = w / 2;
  1145.   y = h / 2;        /* Leave 1/2 blk margin     */
  1146.   style = 0;
  1147.  
  1148.   for( j=0 ; j<3 ; ++j ){        /* Three rows of boxes        */
  1149.     for( i=0 ; i<4 ; ++i ){        /* Four column of boxes     */
  1150.       setfillpattern( &patterns[style][0], MaxColors-1 );
  1151.       bar( x, y, x+w, y+h );        /* Draw the actual box        */
  1152.       rectangle( x, y, x+w, y+h );    /* Outline the box        */
  1153.       itoa( style, buffer, 10 );    /* Convert style 3 to ASCII    */
  1154.       outtextxy( x+(w / 2), y+h+4, buffer );
  1155.       ++style;                /* Go on to next style #    */
  1156.       x += (w / 2) * 3;         /* Go to next column        */
  1157.     }                /* End of coulmn loop        */
  1158.     x = w / 2;                /* Put base back to 1st column    */
  1159.     y += (h / 2) * 3;            /* Advance to next row        */
  1160.   }                    /* End of Row loop        */
  1161.  
  1162.   settextjustify( LEFT_TEXT, TOP_TEXT );
  1163.  
  1164.   Pause();                /* Wait for user's response     */
  1165.  
  1166. }
  1167. /*----------------- RGB colors from HSV model ---------------------------*/
  1168. /* hue    =  pure color of the light
  1169.    sat    =  how much white light is mixed in
  1170.    value  =  max component of RGB */
  1171.  
  1172. void hsv2rgb(float h,float s,float v,RGB *Color)
  1173. {
  1174.   float h1,f,a[7];
  1175.   int   i;
  1176.     h1 = h / 60;
  1177.     i  = h1;
  1178.     f  = h1 - i;
  1179.     a[1] = v;
  1180.     a[2] = v;
  1181.     a[3] = v * (1 - (s*f));
  1182.     a[4] = v * (1 - s);
  1183.     a[5] = a[4];
  1184.     a[6] = v * (1-(s*(1-f)));
  1185.     if (i>4) i = i - 4; else i = i + 2;
  1186.     Color->Red = a[i];
  1187.     if (i>4) i = i - 4; else i = i + 2;
  1188.     Color->Green = a[i];
  1189.     if (i>4) i = i - 4; else i = i + 2;
  1190.     Color->Blue  = a[i];
  1191. }
  1192.  
  1193. void BuildColors(void)
  1194. {
  1195.   float hue,sat,val;
  1196.   RGB ColorValue;
  1197.   int x;
  1198.     hue = 0;
  1199.     sat = 1.0;
  1200.     val = 43.0;
  1201.     for (x=1;  x<255;  x++) {
  1202.         hsv2rgb(hue,sat,val,&ColorValue);
  1203.         Palette_Array[x][red] = ColorValue.Red;
  1204.         Palette_Array[x][grn] = ColorValue.Green;
  1205.         Palette_Array[x][blu] = ColorValue.Blue;
  1206.         hue = hue + 1.8500;
  1207.     }
  1208.     Palette_Array[0][red]     = 0;     /* Set first DAC register to black */
  1209.     Palette_Array[0][grn]     = 0;
  1210.     Palette_Array[0][blu]     = 0;
  1211.     Palette_Array[255][red]   = 0x3f;  /* Set last DAC register to white */
  1212.     Palette_Array[255][grn]   = 0x3f;
  1213.     Palette_Array[255][blu]   = 0x3f;
  1214.     dacpalette(Palette_Array);      /* load DAC registers with new colors */
  1215. }
  1216.  
  1217. /*                                    */
  1218. /*    POLYDEMO: Display a random pattern of polygons on the screen    */
  1219. /*    until the user says enough.                    */
  1220. /*                                    */
  1221.  
  1222. void PaletteDemo(void)
  1223. {
  1224.   int i, j, x, y, color;
  1225.   struct viewporttype vp;
  1226.   int height, width, Pixcell;
  1227.  
  1228.   MainWindow( "Palette Demonstration" );
  1229.   StatusLine( "Press any key to continue, ESC to Abort" );
  1230.  
  1231.   if (getmaxcolor() == 255) Pixcell = 16; else Pixcell = 4;
  1232.   getviewsettings( &vp );
  1233.   width  = (vp.right - vp.left) / Pixcell;    /* get width of the box     */
  1234.   height = (vp.bottom - vp.top) / Pixcell;    /* Get the height of the box    */
  1235.  
  1236.   x = y = 0;                /* Start in upper corner    */
  1237.   color = 0;                /* Begin at 1st color        */
  1238.   BuildColors();
  1239.   for( j=1 ; j<Pixcell+1; ++j ){    /* For 10 rows of boxes     */
  1240.     for( i=1 ; i<Pixcell+1; ++i ){    /* For 15 columns of boxes    */
  1241.       setfillstyle( SOLID_FILL, color);    /* Set the color of box */
  1242.       color = i * j;
  1243.       bar( x, y, x+width, y+height );        /* Draw the box     */
  1244.       x += width + 1;                /* Advance to next col    */
  1245.     }                /* End of COLUMN loop        */
  1246.     x = 0;                /* Goto 1st column        */
  1247.     y += height + 1;            /* Goto next row        */
  1248.   }                    /* End of ROW loop        */
  1249.  
  1250.   while( !kbhit() ){            /* Until user enters a key...    */
  1251.     if (getmaxcolor()==255) setrgbpalette(random(254)+1,random(254)+1,
  1252.                       random(254)+1,random(254)+1);
  1253.   }
  1254.  
  1255.   Pause();                /* Wait for user's response     */
  1256.  
  1257. }
  1258.  
  1259. /*                                    */
  1260. /*    POLYDEMO: Display a random pattern of polygons on the screen    */
  1261. /*    until the user says enough.                    */
  1262. /*                                    */
  1263.  
  1264. #define MaxPts        6        /* Maximum # of pts in polygon    */
  1265.  
  1266. void PolyDemo(void)
  1267. {
  1268.   struct PTS poly[ MaxPts ];        /* Space to hold datapoints    */
  1269.   int color;                /* Current drawing color    */
  1270.   int i;
  1271.  
  1272.   MainWindow( "DrawPoly / FillPoly Demonstration" );
  1273.   StatusLine( "ESC Aborts - Press a Key to stop" );
  1274.  
  1275.   while( !kbhit() ){            /* Repeat until a key is hit    */
  1276.  
  1277.     color = 1 + random( MaxColors-1 );    /* Get a random color # (no blk)*/
  1278.     setfillstyle(1, color );    /* Set a random line style    */
  1279.     setcolor( color );            /* Set the desired color    */
  1280.  
  1281.     for( i=0 ; i<(MaxPts-1) ; i++ ){    /* Determine a random polygon    */
  1282.       poly[i].x = random( MaxX );    /* Set the x coord of point    */
  1283.       poly[i].y = random( MaxY );    /* Set the y coord of point    */
  1284.     }
  1285.  
  1286.     poly[i].x = poly[0].x;        /* last point = first point    */
  1287.     poly[i].y = poly[1].y;
  1288.  
  1289.     fillpoly( MaxPts, (int far *)poly );    /* Draw the actual polygon        */
  1290.   }                    /* End of WHILE not KBHIT    */
  1291.  
  1292.   Pause();                /* Wait for user's response     */
  1293.  
  1294. }
  1295.  
  1296.  
  1297. /*                                    */
  1298. /*    SAYGOODBYE: Give a closing screen to the user before leaving.    */
  1299. /*                                    */
  1300.  
  1301. void SayGoodbye(void)
  1302. {
  1303.   struct viewporttype viewinfo;     /* Structure to read viewport    */
  1304.   int h, w;
  1305.  
  1306.   MainWindow( "== Finale ==" );
  1307.  
  1308.   getviewsettings( &viewinfo );     /* Read viewport settings    */
  1309.   changetextstyle( TRIPLEX_FONT, HORIZ_DIR, 4 );
  1310.   settextjustify( CENTER_TEXT, CENTER_TEXT );
  1311.  
  1312.   h = viewinfo.bottom - viewinfo.top;
  1313.   w = viewinfo.right  - viewinfo.left;
  1314.   outtextxy( w/2, h/2, "That's all, folks!" );
  1315.  
  1316.   StatusLine( "Press any key to EXIT" );
  1317.   getch();
  1318.  
  1319.   cleardevice();            /* Clear the graphics screen    */
  1320.  
  1321. }
  1322.  
  1323. /*                                    */
  1324. /*    PAUSE: Pause until the user enters a keystroke. If the        */
  1325. /*    key is an ESC, then exit program, else simply return.        */
  1326. /*                                    */
  1327.  
  1328. void Pause(void)
  1329. {
  1330.   static char msg[] = "Esc aborts or press a key...";
  1331.   int c;
  1332.  
  1333.   StatusLine( msg );            /* Put msg at bottom of screen    */
  1334.  
  1335.   c = getch();                /* Read a character from kbd    */
  1336.  
  1337.   if( ESC == c ){            /* Does user wish to leave?    */
  1338.     closegraph();            /* Change to text mode        */
  1339.     exit( 1 );                /* Return to OS         */
  1340.   }
  1341.  
  1342.   if( 0 == c ){             /* Did use hit a non-ASCII key? */
  1343.     c = getch();            /* Read scan code for keyboard    */
  1344.   }
  1345.  
  1346.   cleardevice();            /* Clear the screen        */
  1347.  
  1348. }
  1349.  
  1350. /*                                    */
  1351. /*    MAINWINDOW: Establish the main window for the demo and set    */
  1352. /*    a viewport for the demo code.                    */
  1353. /*                                    */
  1354.  
  1355. void MainWindow( char *header )
  1356. {
  1357.   int height;
  1358.  
  1359.   cleardevice();            /* Clear graphics screen    */
  1360.   setcolor( MaxColors - 1 );        /* Set current color to white    */
  1361.   setviewport( 0, 0, MaxX, MaxY, 1 );    /* Open port to full screen    */
  1362.  
  1363.   height = textheight( "H" );           /* Get basic text height        */
  1364.  
  1365.   changetextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );
  1366.   settextjustify( CENTER_TEXT, TOP_TEXT );
  1367.   outtextxy( MaxX/2, 2, header );
  1368.   setviewport( 0, height+4, MaxX, MaxY-(height+4), 1 );
  1369.   DrawBorder();
  1370.   setviewport( 1, height+5, MaxX-1, MaxY-(height+5), 1 );
  1371.  
  1372. }
  1373.  
  1374. /*                                    */
  1375. /*    STATUSLINE: Display a status line at the bottom of the screen.    */
  1376. /*                                    */
  1377.  
  1378. void StatusLine( char *msg )
  1379. {
  1380.   int height;
  1381.  
  1382.   setviewport( 0, 0, MaxX, MaxY, 1 );    /* Open port to full screen    */
  1383.   setcolor( MaxColors - 1 );        /* Set current color to white    */
  1384.  
  1385.   changetextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );
  1386.   settextjustify( CENTER_TEXT, TOP_TEXT );
  1387.   setlinestyle( SOLID_LINE, 0, NORM_WIDTH );
  1388.   setfillstyle( EMPTY_FILL, 0 );
  1389.  
  1390.   height = textheight( "H" );           /* Detemine current height      */
  1391.   bar( 0, MaxY-(height+4), MaxX, MaxY );
  1392.   rectangle( 0, MaxY-(height+4), MaxX, MaxY );
  1393.   outtextxy( MaxX/2, MaxY-(height+2), msg );
  1394.   setviewport( 1, height+5, MaxX-1, MaxY-(height+5), 1 );
  1395.  
  1396. }
  1397.  
  1398. /*                                    */
  1399. /*    DRAWBORDER: Draw a solid single line around the current     */
  1400. /*    viewport.                            */
  1401. /*                                    */
  1402.  
  1403. void DrawBorder(void)
  1404. {
  1405.   struct viewporttype vp;
  1406.  
  1407.   setcolor( MaxColors - 1 );        /* Set current color to white    */
  1408.  
  1409.   setlinestyle( SOLID_LINE, 0, NORM_WIDTH );
  1410.  
  1411.   getviewsettings( &vp );
  1412.   rectangle( 0, 0, vp.right-vp.left, vp.bottom-vp.top );
  1413.  
  1414. }
  1415.  
  1416. /*                                    */
  1417. /*    CHANGETEXTSTYLE: similar to settextstyle, but checks for    */
  1418. /*    errors that might occur whil loading the font file.        */
  1419. /*                                    */
  1420.  
  1421. void changetextstyle(int font, int direction, int charsize)
  1422. {
  1423.   int ErrorCode;
  1424.  
  1425.   graphresult();            /* clear error code        */
  1426.   settextstyle(font, direction, charsize);
  1427.   ErrorCode = graphresult();        /* check result         */
  1428.   if( ErrorCode != grOk ){        /* if error occured        */
  1429.     closegraph();
  1430.     printf(" Graphics System Error: %s\n", grapherrormsg( ErrorCode ) );
  1431.     exit( 1 );
  1432.   }
  1433. }
  1434.  
  1435. /*                                    */
  1436. /*    GPRINTF: Used like PRINTF except the output is sent to the    */
  1437. /*    screen in graphics mode at the specified co-ordinate.        */
  1438. /*                                    */
  1439.  
  1440. int gprintf( int *xloc, int *yloc, char *fmt, ... )
  1441. {
  1442.   va_list  argptr;            /* Argument list pointer    */
  1443.   char str[140];            /* Buffer to build sting into    */
  1444.   int cnt;                /* Result of SPRINTF for return */
  1445.  
  1446.   va_start( argptr, fmt );        /* Initialize va_ functions    */
  1447.  
  1448.   cnt = vsprintf( str, fmt, argptr );    /* prints string to buffer    */
  1449.   outtextxy( *xloc, *yloc, str );    /* Send string in graphics mode */
  1450.   *yloc += textheight( "H" ) + 2;       /* Advance to next line         */
  1451.  
  1452.   va_end( argptr );            /* Close va_ functions        */
  1453.  
  1454.   return( cnt );            /* Return the conversion count    */
  1455.  
  1456. }
  1457.